package bulk_param.numspe.controller;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.JOptionPane;

import bulk_param.dao.InactivationPointDao;
import bulk_param.dao.ModifNumSpeDao;
import bulk_param.dao.VerificationMaxPointIDDao;
import bulk_param.modele.NumSpePointModel;
import bulk_param.view.NumSpe_Modif_View;


public class ButtonNumSpeModifController implements ActionListener{
	
	private NumSpe_Modif_View numSpe_Modif_View;
	private ModifNumSpeDao modifNumSpeDao;
	private InactivationPointDao inactivationPointDao;
	private NumSpePointModel numSpePointModel;
	private VerificationMaxPointIDDao verificationMaxPointIDDao;
	private String oldTarif="";
	private String point="";

	
	
	public ButtonNumSpeModifController(NumSpe_Modif_View numSpe_Modif_View){
		this.numSpe_Modif_View=numSpe_Modif_View;
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		//Remise des contours champs à gris	
		numSpe_Modif_View.txtZABPQ.setBorder(BorderFactory.createLineBorder(Color.lightGray));
		numSpe_Modif_View.txtOldTarif.setBorder(BorderFactory.createLineBorder(Color.lightGray));
		numSpe_Modif_View.widerDropDownCombo.setBorder(BorderFactory.createLineBorder(Color.lightGray));
		numSpe_Modif_View.dateChooser.setBorder(BorderFactory.createLineBorder(Color.lightGray));		
		numSpePointModel = new NumSpePointModel();
		
		
			verificationMaxPointIDDao = new VerificationMaxPointIDDao();
			if(verificationMaxPointIDDao.isMaxPointIDEgaux()){
			inactivationPointDao= new InactivationPointDao();
			modifNumSpeDao= new ModifNumSpeDao();
			point="33"+numSpe_Modif_View.txtZABPQ.getText().substring(1);
			Timestamp dateInBase =null;
			Timestamp dateInBaseInactive =null;
			Date theDate=numSpe_Modif_View.dateChooser.getDate();
			if(numSpe_Modif_View.btValider.isEnabled()){
			if(numSpe_Modif_View.dateChooser.getDate()==null){
				JOptionPane.showMessageDialog(null, "La date de modification \nest obligatoire", "Attention", JOptionPane.WARNING_MESSAGE);
				numSpe_Modif_View.dateChooser.setBorder(BorderFactory.createLineBorder(Color.red));						
			}
			if(numSpe_Modif_View.widerDropDownCombo.getSelectedIndex()==0){
				JOptionPane.showMessageDialog(null, "Le tarif est obligatoire", "Attention", JOptionPane.WARNING_MESSAGE);				
				numSpe_Modif_View.widerDropDownCombo.setBorder(BorderFactory.createLineBorder(Color.red));	
				}
			}
			if(numSpe_Modif_View.dateChooser.getDate()!=null && numSpe_Modif_View.widerDropDownCombo.getSelectedIndex()!=0){
			try {
				dateInBase=modifNumSpeDao.dateInBaseActive(point);
				System.out.println("date en base pour modification : "+dateInBase);
				System.out.println("ma date : "+theDate);
				if(modifNumSpeDao.isDateBeforeActive(point, theDate)==false){
					int option = JOptionPane.showConfirmDialog(null, "Etes vous sur de vouloir \nmodifier le point "+point+" ?", "Modification point", 0, 3);
		            if(option != 1 && option == 0){
						inactivationPointDao.inactivePointZABPQ(point, theDate, dateInBase);
						int entrepriseCPTL = 1;
						int entrepriseDARTY = 2;
						String access_region = modifNumSpeDao.numberJurisdiction(numSpe_Modif_View.widerDropDownCombo.getSelectedItem().toString());
						String pointZABPQ = "33"+numSpe_Modif_View.txtZABPQ.getText().substring(1);
						Date date = numSpe_Modif_View.dateChooser.getDate();
						String zLT = numSpe_Modif_View.widerDropDownCombo.getSelectedItem().toString();
						int pointClass = Integer.valueOf(access_region.trim());
						int isPrefix=0;
						if(numSpe_Modif_View.txtZABPQ.getText().matches(numSpePointModel.getNUM_0800_REGEX())){
							isPrefix=1;
						}else{
							isPrefix=0;
						}
						System.out.println("MES VALEURS DENTRES : ");
						System.out.println("access_region correspondant au tarif : "+access_region);
						System.out.println("ZABPQ : "+pointZABPQ);
						System.out.println("date : "+date);
						System.out.println("ZLT : "+zLT);
						dateInBaseInactive=modifNumSpeDao.dateInBaseInactive(point);
						System.out.println("je passe par la modification");
						String success1=modifNumSpeDao.openPointNumSpeplus1sec(pointClass, pointZABPQ,isPrefix, date, access_region,zLT, entrepriseCPTL,dateInBaseInactive,"M");					
						String success2=modifNumSpeDao.openPointNumSpeplus1sec(pointClass, pointZABPQ,isPrefix, date,access_region, zLT, entrepriseDARTY,dateInBaseInactive,"M");					
						if(success1=="ko" || success2=="ko" ){
							if(success1=="ko"){
								String msg= "Le point : " +pointZABPQ+ "\nen 'point_category'=1 n'a pas été créé";
								JOptionPane.showMessageDialog(null, msg, "Message", JOptionPane.INFORMATION_MESSAGE);										
							}
							if(success2=="ko"){
								String msg= "Le point : " +pointZABPQ+ "\nen 'point_category'=2 n'a pas été créé";
								JOptionPane.showMessageDialog(null, msg, "Message", JOptionPane.INFORMATION_MESSAGE);							
							}									
						}
						if(success1=="ok" && success2=="ok" ){
							String succesMsg= "Le point : " +pointZABPQ + "\na été modifié avec succès";
							JOptionPane.showMessageDialog(null, succesMsg, "Message", JOptionPane.INFORMATION_MESSAGE);	
							}	
						numSpe_Modif_View.txtOldTarif.setText(null);
						numSpe_Modif_View.dateEditor.setText(numSpe_Modif_View.selectDate);
						numSpe_Modif_View.dateChooser.setEnabled(false);
						numSpe_Modif_View.widerDropDownCombo.setSelectedIndex(0);
						numSpe_Modif_View.btValider.setEnabled(false);
						modifNumSpeDao.closeConnection();
		            }else{
		            	
		            }
				}else{
					String dateMsg="Vous devez choisir une date \négale ou postérieure à la date : "+new java.sql.Date(dateInBase.getTime());
					JOptionPane.showMessageDialog(null, dateMsg, "Attention", JOptionPane.WARNING_MESSAGE);	
				}		
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				}	
			}
			
		
		
		if(arg0.getSource().equals(numSpe_Modif_View.btSearch)){
			
			
			if(!numSpe_Modif_View.txtZABPQ.getText().isEmpty() && !numSpe_Modif_View.txtZABPQ.getText().matches(numSpePointModel.getZABPQ_REGEX())){
				numSpe_Modif_View.txtZABPQ.setBorder(BorderFactory.createLineBorder(Color.red));
				JOptionPane.showMessageDialog(null,numSpePointModel.getZABPQ_REGEX_MESSAGE(),"Attention",JOptionPane.WARNING_MESSAGE);							
			}else if(numSpe_Modif_View.txtZABPQ.getText().isEmpty()){
				numSpe_Modif_View.txtZABPQ.setBorder(BorderFactory.createLineBorder(Color.red));
				JOptionPane.showMessageDialog(null,"Veuillez renseigner le point(0ZABPQ)","Attention",JOptionPane.WARNING_MESSAGE);							
			}else{
				modifNumSpeDao= new ModifNumSpeDao();
				point="33"+numSpe_Modif_View.txtZABPQ.getText().substring(1);
				if (modifNumSpeDao.isPointExist(point)){
					oldTarif=modifNumSpeDao.oldTarif(point);
					numSpe_Modif_View.txtOldTarif.setText(oldTarif);
					numSpe_Modif_View.dateChooser.setEnabled(true);
					numSpe_Modif_View.widerDropDownCombo.setEnabled(true);
					numSpe_Modif_View.btValider.setEnabled(true);
					modifNumSpeDao.closeConnection();
					if(numSpe_Modif_View.lbZABPQ.getText().equalsIgnoreCase("Point(03BPQ) : ")) {
						numSpe_Modif_View.widerDropDownCombo.removeAllItems();
						numSpe_Modif_View.widerDropDownCombo.addItem(" ");
				        for (int i=0;i<numSpe_Modif_View.listTarification.size();i++){
				        	if(numSpe_Modif_View.listTarification.get(i).substring(3, 4).equalsIgnoreCase("c")
				        	|| numSpe_Modif_View.listTarification.get(i).substring(3, 4).equalsIgnoreCase("j")
				        	|| numSpe_Modif_View.listTarification.get(i).substring(0, 1).equalsIgnoreCase("U")
				        	|| numSpe_Modif_View.listTarification.get(i).substring(0, 1).equalsIgnoreCase("z")){
				        	numSpe_Modif_View.widerDropDownCombo.addItem(numSpe_Modif_View.listTarification.get(i));
				        	}
				        }
					}
					if(numSpe_Modif_View.lbZABPQ.getText().equalsIgnoreCase("Point(0118 XYZ) : ")){
						if(numSpe_Modif_View.txtZABPQ.getText().length()<=5){
							numSpe_Modif_View.widerDropDownCombo.removeAllItems();
							numSpe_Modif_View.widerDropDownCombo.addItem(" ");
					        for (int i=0;i<numSpe_Modif_View.listTarification.size();i++){
					        	if(numSpe_Modif_View.listTarification.get(i).substring(3, 4).equalsIgnoreCase("c")
					        	|| numSpe_Modif_View.listTarification.get(i).substring(3, 4).equalsIgnoreCase("j")
					        	|| numSpe_Modif_View.listTarification.get(i).substring(0, 1).equalsIgnoreCase("U")
					        	|| numSpe_Modif_View.listTarification.get(i).substring(0, 1).equalsIgnoreCase("z")){
					        	numSpe_Modif_View.widerDropDownCombo.addItem(numSpe_Modif_View.listTarification.get(i));
					        	}
					        }
						}else{
						numSpe_Modif_View.widerDropDownCombo.removeAllItems();
						numSpe_Modif_View.widerDropDownCombo.addItem(" ");
				        for (int i=0;i<numSpe_Modif_View.listTarification.size();i++){
				        	if(numSpe_Modif_View.listTarification.get(i).substring(0,1).equalsIgnoreCase("R")
		        			|| numSpe_Modif_View.listTarification.get(i).substring(3, 4).equalsIgnoreCase("j")
				        	|| numSpe_Modif_View.listTarification.get(i).substring(0, 1).equalsIgnoreCase("U")
				        	|| numSpe_Modif_View.listTarification.get(i).substring(0, 1).equalsIgnoreCase("z")){
				        	numSpe_Modif_View.widerDropDownCombo.addItem(numSpe_Modif_View.listTarification.get(i));
				        	}
				        }
					}
					}if(numSpe_Modif_View.lbZABPQ.getText().equalsIgnoreCase("Point(0800) : ")){
						numSpe_Modif_View.widerDropDownCombo.removeAllItems();
						numSpe_Modif_View.widerDropDownCombo.addItem(" ");
				        for (int i=0;i<numSpe_Modif_View.listTarification.size();i++){
				        	if(numSpe_Modif_View.listTarification.get(i).substring(3, 4).equalsIgnoreCase("8")
		        			|| numSpe_Modif_View.listTarification.get(i).substring(3, 4).equalsIgnoreCase("j")
				        	|| numSpe_Modif_View.listTarification.get(i).substring(0, 1).equalsIgnoreCase("U")){
				        	numSpe_Modif_View.widerDropDownCombo.addItem(numSpe_Modif_View.listTarification.get(i));
				        	}
				        }
					}		
				}else{
					JOptionPane.showMessageDialog(null,"Le point "+point+"\nn'éxiste pas","Attention",JOptionPane.WARNING_MESSAGE);							
				}
			}
			
		}
		
		if(arg0.getSource().equals(numSpe_Modif_View.btAnnuler)){		
			numSpe_Modif_View.txtZABPQ.setText(null);
			numSpe_Modif_View.txtOldTarif.setText(null);
			numSpe_Modif_View.dateEditor.setText(numSpe_Modif_View.selectDate);
			numSpe_Modif_View.dateChooser.setEnabled(false);
			numSpe_Modif_View.widerDropDownCombo.setSelectedIndex(0);
			numSpe_Modif_View.btValider.setEnabled(false);
			}
		}				
		else{
			String message="L'opération a été annulée.\nUn problème lié au max point_id en est la cause";
			JOptionPane.showMessageDialog(null, message, "Attention", JOptionPane.WARNING_MESSAGE);		
		}
	}
}	


